#include <iostream>
#include    "Board.hpp"
#include    "Eat.hpp"


Eat::Eat()
{
}

Eat::~Eat()
{
}

const QList<int>&     Eat::getBeg() const
{
    return _beg;
}
void    Eat::checkLine(int pos, int player, int enemy, int mask)
{
    if (pos == -1)
        return;
    qDebug("checkLine in");
    if ((pos & 7) == enemy)
    {
        /*
        qDebug("probabilite de trouver une eat");
        std::cout << "mask= " << mask << std::endl;
        std::cout << "pos = " << pos << std::endl;
        std::cout << "pos&mask= " << (pos&mask)  << std::endl;
        std::cout << ((pos & mask) >> Board::getInstance()->getMaskPlace(mask)) << std::endl;
        */
        if (((pos & mask) >> Board::getInstance()->getMaskPlace(mask)) == player)
        {
            //qDebug("omg la proba");
            mask = mask >= 229376 ? mask >> 12 : mask << 12;
            if (((pos & mask) >> Board::getInstance()->getMaskPlace(mask)) == enemy)
                qDebug("Ok on a trouve une putain de proba");
        }
    }
}

void    Eat::doesEat(int pos)
{
    int player = (pos & 0xFF0000) >> 16;
    int enemy = (player == 1) ? 2 : 1;
    int x = (pos & 0xFF00) >> 8;
    int y = pos & 0xFF;

    qDebug("Entree doesEat");
    Board*  board = Board::getInstance();
    pos = board->getAt(x, y);
	// de 1 a 8
    for (int mask = 56 ; mask < 939524096 ; mask <<= 3)
        checkLine(board->getAt(x + board->getMaskMove(false, mask) * 2,
                               y + board->getMaskMove(true, mask) * 2),
                  player, enemy, mask);

    if (_beg.size())
        board->deleteBetween(pos, this->_beg);
    qDebug("Sortie doesEat");
}
